<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true" @submit.native.prevent>
      <el-row>
        <el-col :span="19">
          <!-- <el-form-item label="任务单号:">
            <el-input v-model="searchForm.jobId" style="width:165px" placeholder="任务单号" />
          </el-form-item>
          <el-form-item label="项目名称:">
            <el-input v-model="searchForm.projectName" style="width:165px" placeholder="项目名称" />
          </el-form-item> -->

          <el-form-item>
            <el-input v-model="searchForm.fuzzy" style="width:350px" placeholder="项目名称/委托单位/受检单位/报告编号/任务单号" clearable @keyup.enter.native="handleSearch" />
          </el-form-item>
          <el-form-item label="报告状态:">
            <el-select v-model="searchForm.reportStatus" clearable style="width:165px" filterable placeholder="报告状态">
              <el-option
                v-for="item in Object.keys(REPORT_STATUS)"
                :key="item"
                :label="REPORT_STATUS[item]"
                :value="item"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" class="iconfont icon-chaxun1" @click="handleSearch"> 查询</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="5" class="search-right">
          <el-button type="text" icon="el-icon-refresh" @click="handleSearch">刷新</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      border
      :data="reportData"
      stripe
      highlight-current-row
      :header-cell-style="{background:'#ECF3FC'}"
    >
      <el-table-column type="index" width="50" label="序号" align="center" />
      <el-table-column prop="reportCode" label="报告编号" width="140px" align="center" />
      <el-table-column prop="jobId" label="任务单号" width="140px" align="center" />
      <el-table-column prop="projectName" label="项目名称" align="center" />
      <el-table-column prop="consignorName" label="委托单位" align="center" />
      <el-table-column prop="inspectionName" label="受检单位" align="center" />
      <el-table-column prop="reportStatus" label="报告状态" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.reportStatus|statusQualityFilter">
            {{ REPORT_STATUS[row.reportStatus] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="reportMakeTime" label="生成时间" width="160px" align="center" />
      <el-table-column label="操作" align="center" width="185px">
        <template slot-scope="{row}">
          <el-button type="text" @click="handleDetail(row)">查看</el-button>

          <!-- <div v-if="row.reportStatus > 1" class="erc-divider-vertical" />
          <el-dropdown v-if="row.reportStatus > 1">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-link v-if="row.reportStatus>1" :underline="false" @click="handleDownload(row)">
                <el-dropdown-item>最新报告下载</el-dropdown-item>
              </el-link>
              <el-link v-if="row.reportStatus>1" :underline="false" @click="handleOldDownload(row)">
                <el-dropdown-item>原始报告下载</el-dropdown-item>
              </el-link>
            </el-dropdown-menu>
          </el-dropdown> -->
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="searchForm.pageNumber"
        :limit.sync="searchForm.pageSize"
        @pagination="handleSearch"
      />
    </div>
  </div>
</template>

<script>
const REPORT_STATUS = {
  // '0': '待分配',
  // '1': '未生成',
  // '2': '制作中',
  // '3': '待提交审批',
  // '4': '待审核',
  '5': '待签发',
  '6': '已完成',
  // '7': '已驳回'

}
import Pagination from '@/components/Pagination'
import * as reportServer from '@/api/report'
import { mapGetters } from 'vuex'
import waves from '@/directive/waves' // waves directive
import checkPermission from '@/utils/permission' // 权限判断函数
import * as offermonitorServer from "@/api/offermonitor";
export default {
  components: { Pagination },
  directives: { waves },
  filters: {
    statusQualityFilter(status) {
      if (status < 3) {
        return 'info'
      } else if (status < 6) {
        return ''
      } else if (status === '6') {
        return 'success'
      } else {
        return 'danger'
      }
    }
  },
  data() {
    return {
      REPORT_STATUS,
      searchForm: {
        fuzzy: '',
        jobId: '',
        projectName: '',
        reportStatus: '',
        pageNumber: 1,
        pageSize: 10
      },
      total: 0,
      reportData: [],
      hideLogoUploadEdit: false,
      hideUploadEdit: false,
      downloadLoading: false

    }
  },
  computed: {
    ...mapGetters(['platformPositionId', 'userId'])
  },
  created() {
    this.handleSearch()
  },
  methods: {
    checkPermission,
    handleSearch() {
      reportServer.findHisReportInfosByCondition(this.searchForm).then(res => {
        if (res.code === 200 && res.result) {
          this.reportData = res.result.records || []
          this.total = res.result.total
        }
      })
    },
    handleAddReport(row) {
      this.downloadLoading = true
      reportServer.genReport(row.reportCode).then(result => {
        if (result.code === 200) {
          this.$notify({
            title: '成功',
            message: result.message,
            type: 'success'
          })
          this.handleSearch()
        } else {
          this.$notify({
            title: '失败',
            message: result.message,
            type: 'error'
          })
        }
        this.downloadLoading = false
      }).catch(() => {
        this.downloadLoading = false
      })
    },
    handleDetail(row) {
      offermonitorServer.trainFile({ type: 4, id: row.jobId }).then(res => {
        if (res.code === 200) {
          if (res.result.length != 0) {
            var arr = res.result.length - 1;
            var url = res.result[arr].url;
            window.open(url);
          } else {
            this.$message.error("上传报告为空！");
          }
        }
      });
    },
    handleDownload(row) {
      var url = this.$store.getters.sysBaseUrl + '/file/report/' + row.reportFileId
      window.open(url)
    },
    handleOldDownload(row) {
      var url = this.$store.getters.sysBaseUrl + '/file/report/' + row.reportCode + '.docx'
      window.open(url)
    }

  }
}
</script>

<style scoped lang="scss" type="text/scss">
  .search_div {
    >div {
      &:nth-child(1){
        margin-right: 10px;
      }
      display: inline-block;
    }
  }

  .search_input {
    width: 200px;
    margin-bottom: 5px;
  }
  .search-right {
    text-align: right;
    padding-right: 25px;
  }
  .item {
    font-size: 16px;
  }
  ::v-deep .el-card .el-card__header {
    padding: 5px 20px;
    background: #F3F2F2;
  }
   .info-table-new {
    font-size: 14px;
    width: 100%;
    text-align: center;
    margin-top: 10px;
    border-top: 1px solid #ececec;
    border-left: 1px solid #ececec;
    line-height: 35px;
    td {
      border-bottom: 1px solid #ececec;
      border-right: 1px solid #ececec;
      width: 25%;
      height: 35px;
      // &:nth-child(2n + 1) {
      //   width: 25%;
      //   background: #f5f5f5;
      // }
    }
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
  /deep/.hide .el-upload--picture-card {
    display: none;
  }
</style>
